"use strict";
cc._RF.push(module, '2d6ddeG9ltHo4wMm1+inwh7', 'WXLogin');
// Script/Ssg/API/WX/WXLogin.js

'use strict';

var WXLogin = cc.Class({

    statics: {

        startLogin: function startLogin(back) {
            if (SystemInfo.getPlatform() != 'weChatGame') {
                back(true);
                return;
            }
            var self = this;
            wx.login({
                success: function success(res) {
                    var js_code = res.code;
                    //这里也是皮新加的
                    var param = '/login/getOpenId?js_code=' + js_code;

                    HttpUtil.nafosJsonGet(param, function (responseBack) {
                        Log.debug('服务器去请求的微信信息：' + responseBack);
                        var wxServerBack = JSON.parse(responseBack);
                        var openid = wxServerBack.openid;
                        var unionid = wxServerBack.unionid;
                        var sessionKey = wxServerBack.session_key;
                        if (responseBack) {
                            self.getUserInfo(function (isSuccess, result) {
                                if (isSuccess) {
                                    var userInfo = result.userInfo;
                                    UserInfo.openId = openid;
                                    UserInfo.userName = userInfo.nickName;
                                    UserInfo.userImg = userInfo.avatarUrl;
                                    back(true);
                                } else {
                                    back(false);
                                }
                            });
                        } else {
                            back(false, responseBack.errMsg);
                        }
                    });
                }
            });
        },

        /**
         *  获取登录用户的信息
         */
        getUserInfo: function getUserInfo(back) {
            var self = this;
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
                success: function success(res) {
                    // 可以将 res 发送给后台解码出 unionId
                    // var userInfo = res.userInfo
                    // var nickName = userInfo.nickName
                    // var avatarUrl = userInfo.avatarUrl
                    // var gender = userInfo.gender //性别 0：未知、1：男、2：女
                    // var province = userInfo.province
                    // var city = userInfo.city
                    // var country = userInfo.country

                    back(true, res);
                },
                fail: function fail(res) {
                    // iOS 和 Android 对于拒绝授权的回调 errMsg 没有统一，需要做一下兼容处理
                    if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('auth denied') > -1) {
                        // 处理用户拒绝授权的情况
                        cc.log('用户拒绝授权');
                        back(false, '用户拒绝授权');
                    }
                }
            });
        }

    }
});

module.exports = WXLogin;

cc._RF.pop();